﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using Microsoft.LightSwitch;
using Microsoft.LightSwitch.Threading;
using System.Collections;

namespace LightSwitchApplication.Server
{
    public partial class SiteMapDataService : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        [WebMethod]
        public static void UpdateAddress(string AddressId, string Latitude, string Longitude)
        {
            LightSwitchApplication.Server.DataAccess.UpdateAddress(AddressId, Latitude, Longitude);
            //Dispatchers.Main.Invoke(() =>
            //{
            //    try
            //    {
            //        using (var dwspace = new DataWorkspace())
            //        {
            //            Address address = dwspace.ApplicationData.Addresses.Where(add => add.Id.ToString() == AddressId).FirstOrDefault();
            //            if (address == null) return;
            //            address.Latitude = double.Parse(Latitude);
            //            address.Longitude = double.Parse(Longitude);
            //            dwspace.ApplicationData.SaveChanges();
            //        }
            //    }
            //    catch (Exception ex)
            //    {
            //    }
            //});

        }

        [WebMethod]
        public static ArrayList GetAddress(string AddressId)
        {
            if (!string.IsNullOrEmpty(AddressId))
            {
                string fullAddress = LightSwitchApplication.Server.DataAccess.GetAddress(AddressId);
                return new ArrayList()
                {
                    new { Value = fullAddress, Display = "FullAddress" }
                };
            }
            else
            {
                return null;
            }
        }

        [WebMethod]
        public static ArrayList SimpleDecoder(string EncodedString)
        {
            if (!string.IsNullOrEmpty(EncodedString))
            {
                return new ArrayList()
                {
                    new { Value = Utils.SimpleDecoder(EncodedString, "=="), Display = "OriginalValue" }
                };
            }
            else
            {
                return null;
            }
        }
    }
}